package CutLoad.components.container
{
	/**
	 * 缓动跑马灯效果容器，
	 * @author smallerbird mail:smallerbird@gmail.com  site:www.mkigm.com QQ群:49983196
	 * 
	 */
	import CutLoad.Base;
	import CutLoad.cookies.Painter;
	import CutLoad.transitions.Easing;
	
	import flash.display.Sprite;
	import flash.events.Event;
	
	public class CMarquee extends Base
	{
		public static function getMarqueeForCreateBody(arrBase:Array,maxW:Number,maxH:Number,speed:Number=5,xspace:Number=5,offset:Number=0.2):CMarquee{
			var body:ContainerH=ContainerH.getSimple({w:10,h:10,isAuto:false,xspace:xspace,yspace:5});
			body.appendSome(arrBase);
			body.setTakeWH();
			return new CMarquee(body,maxW,maxH,speed,offset);
		}
		private var spMask:Base;
		private var maxW:Number,maxH:Number,offset:Number,speed:Number,body:Sprite;
		private var easing:Easing=null;
		//可以移动的距离
		private var canMove:Number;
		private var canMove_x:Number;
		public function CMarquee(body:Sprite,maxW:Number,maxH:Number,speed:Number,offset:Number=0.2)
		{
			super();
			this.maxW=maxW;
			this.maxH=maxH;
			this.offset=offset;
			this.speed=speed;
			this.body=body;
			this.canMove=body.width-this.maxW;
			canMove_x=0;
			spMask=Painter.drawRect(maxW,maxH);
			if(body.parent==null){
				this.addChild(spMask);
				this.addChild(body);
			}else{
				body.parent.addChild(spMask);
			}
			body.mask=spMask;
			spMask.x=body.x;
			spMask.y=body.y;
			//
			this.addEventListener(Event.ENTER_FRAME,funEnterFrame);
		}
		public function isBodyBigMe():Boolean{
			if(body.width>this.maxW) return true;
			return false;
		}
		private function funEnterFrame(e:Event):void{
			if(easing!=null) easing.enterFrame();
		}
		public function moveLeft():void{
			if(body.width<=this.maxW){
				return;
			}
			if(easing!=null){
				if(easing.state==1){
					return ;
				}	
			}
			//在容许的范围移动
			canMove_x+=this.speed;
			if(canMove_x>this.canMove+this.speed){
				canMove_x=this.canMove+this.speed
				return;
			}
			this.left();
		}
		public function moveRight():void{
			if(body.width<=this.maxW){
				return;
			}
			if(easing!=null){
				if(easing.state==1){
					return ;
				}	
			}
			//在容许的范围移动
			canMove_x-=this.speed;
			if(canMove_x<0){
				canMove_x=0
				return;
			}
			this.right()
		}
		private function left():void{
			easing=new Easing(body.x,body.x-this.speed,1,this.offset);
			easing.setFunEnterFrame(function(num:Number):void{
				body.x=num;
			});
			easing.start();
		}
		private function right():void{
			easing=new Easing(body.x,body.x+this.speed,1,this.offset);
			easing.setFunEnterFrame(function(num:Number):void{
				body.x=num;
			});
			easing.start();
		}
	}
}